<template>
  <a-modal
    title="编辑三方应用"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="应用名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入应用名称" v-decorator="['name', {rules: [{required: true, message: '请输入应用名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="应用图标"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入应用图标" v-decorator="['imageUrl', {rules: [{required: true, message: '请输入应用图标！'}]}]" />
        </a-form-item>
        <a-form-item
          label="是否可用"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入是否可用" v-decorator="['isEnable', {rules: [{required: true, message: '请输入是否可用！'}]}]" />
        </a-form-item>
        <a-form-item
          label="描述"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入描述" v-decorator="['description', {rules: [{required: true, message: '请输入描述！'}]}]" />
        </a-form-item>
        <a-form-item
          label="数据来源"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select
            v-decorator="['orgCode', {rules: [{required: true, message: '请选择数据来源！'}]}]"
            placeholder="请选择数据来源" >
            <a-select-option
              v-for="item in orgList"
              :key="item.code"
              :value="item.code"
            >{{ item.name }}
            </a-select-option>
          </a-select>

        </a-form-item>
        <a-form-item
          label="element图标名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input placeholder="请输入element图标名称" v-decorator="['iconName', {rules: [{required: true, message: '请输入element图标名称！'}]}]" />
          <a href="https://element.eleme.cn/#/zh-CN/component/icon" target="_blank">element图标网址</a>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { biApplicationEdit } from '@/api/modular/main/biapplication/biApplicationManage'
  import { getOrgList } from '@/api/modular/system/orgManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        orgList: [],
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        this.findOrgList()
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              name: record.name,
              imageUrl: record.imageUrl,
              isEnable: record.isEnable,
              orgCode: record.orgCode,
              iconName: record.iconName,
              describe: record.describe
            }
          )
        }, 100)
      },
      // 组织列表
      findOrgList() {
        getOrgList({}).then((res) => {
          this.orgList = res.data
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            biApplicationEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
